﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Common;
using XF.Meeting.Entity.CommonEntity;
using XF.Meeting.Entity.Query;
using XF.Meeting.Entity.View;
using XF.Meeting.IDal;
using XF.Sugar.IService.own;

namespace XF.Meeting.Service
{
    public class ReportService : BaseService, IReportService
    {
        private IReportDal dal = null;
        private ISettingDal setdal = null;

        public ReportService(string Operator, string OperaIP)
        {
            this.Operator = Operator;
            this.OperaIP = OperaIP;
            if (dal == null)
            {
                lock (this.OperaIP)
                {
                    if (dal == null)
                    {
                        dal = DalFactory.DalFactory.GetReportDal();
                    }
                }
            }
            if (setdal == null)
            {
                lock (this.OperaIP)
                {
                    if (setdal == null)
                    {
                        setdal = DalFactory.DalFactory.GetSettingDaL();
                    }
                }
            }
        }

        private UEntity.LogEntity.ELogRecordEntity LogEntity = new UEntity.LogEntity.ELogRecordEntity();


        /// <summary>
        /// 获取会议室统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<MeetingRoomReport>> GetMeetingRoomReport(ReportQuery query)
        {
            PageList<MeetingRoomReport> list = new PageList<MeetingRoomReport>();
            List<MeetingRoomReport> listReport = new List<MeetingRoomReport>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetMeetingRoomReport";
            LogEntity.OperateObject = "GetMeetingRoomReport";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<ReportQuery>(query);
            try
            {
                query.BeginTime = Convert.ToDateTime(query.Begin + " 00:00:01");
                query.EndTime = Convert.ToDateTime(query.End + " 23:59:59");
                var (dt, RecordCount, PageCount) = await dal.GetMeetingRoomReport(query, out strError);

                if (strError == "")
                {
                    var time = await setdal.GetSubscribe(out strError);
                    var subscribe = TableToList.ToList<SubscribeInfo>(time);
                    var subscribeInfo = subscribe[0];
                    var days = (query.EndTime - query.BeginTime).Days;
                    var totalMinutes = (subscribeInfo.SubscribeEnd - subscribeInfo.SubscribeBegin) * 60 * days;

                    listReport = TableToList.ToList<MeetingRoomReport>(dt);
                    foreach (var item in listReport)
                    {
                        double Efficiency = (double)item.TimeLengthSum / totalMinutes;
                        item.UseEfficiency = Efficiency.ToString("0%");
                    }
                    list.List = listReport;
                    list.RecordCount = RecordCount;
                    list.PageCount = PageCount;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetMeetingRoomReport Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }
        /// <summary>
        /// 获取人员统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<PersonMeetingReport>> GetPersonMeetingReport(ReportQuery query)
        {
            PageList<PersonMeetingReport> list = new PageList<PersonMeetingReport>();
            List<PersonMeetingReport> listReport = new List<PersonMeetingReport>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetPersonMeetingReport";
            LogEntity.OperateObject = "GetPersonMeetingReport";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<ReportQuery>(query);
            try
            {
                query.BeginTime = Convert.ToDateTime(query.Begin + " 00:00:01");
                query.EndTime = Convert.ToDateTime(query.End + " 23:59:59");
                var (dt, RecordCount, PageCount) = await dal.GetPersonMeetingReport(query, out strError);
                if (strError == "")
                {
                    list.List = TableToList.ToList<PersonMeetingReport>(dt);
                    list.RecordCount = RecordCount;
                    list.PageCount = PageCount;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetPersonMeetingReport Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }
        /// <summary>
        /// 获取部门统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<DeptMeetingReport>> GetDeptMeetingReport(ReportQuery query)
        {
            PageList<DeptMeetingReport> list = new PageList<DeptMeetingReport>();
            List<DeptMeetingReport> listReport = new List<DeptMeetingReport>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetDeptMeetingReport";
            LogEntity.OperateObject = "GetDeptMeetingReport";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<ReportQuery>(query);
            try
            {
                query.BeginTime = Convert.ToDateTime(query.Begin + " 00:00:01");
                query.EndTime = Convert.ToDateTime(query.End + " 23:59:59");
                var (dt, RecordCount, PageCount) = await dal.GetDeptMeetingReport(query, out strError);
                if (strError == "")
                {
                    list.List = TableToList.ToList<DeptMeetingReport>(dt);
                    list.RecordCount = RecordCount;
                    list.PageCount = PageCount;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetDeptMeetingReport Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }
        /// <summary>
        /// 获取部门人员统计报表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<DeptPersonMeetingReport>> GetDeptPersonMeetingReport(ReportQuery query)
        {
            PageList<DeptPersonMeetingReport> list = new PageList<DeptPersonMeetingReport>();
            List<DeptPersonMeetingReport> listReport = new List<DeptPersonMeetingReport>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetDeptMeetingReport";
            LogEntity.OperateObject = "GetDeptMeetingReport";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<ReportQuery>(query);
            try
            {
                query.BeginTime = Convert.ToDateTime(query.Begin + " 00:00:01");
                query.EndTime = Convert.ToDateTime(query.End + " 23:59:59");
                var (dt, RecordCount, PageCount) = await dal.GetDeptPersonMeetingReport(query, out strError);
                if (strError == "")
                {
                    list.List = TableToList.ToList<DeptPersonMeetingReport>(dt);
                    list.List.ForEach(i =>
                    {
                        i.Date = i.BeginTime.ToString("yyyy-MM-dd");
                        i.Begin = i.BeginTime.ToString("HH:mm");
                        i.End = i.EndTime.ToString("HH:mm");

                        switch (i.ConferenceType)
                        {
                            case 0: i.ConferenceTypeName = "纪要未上传"; break;
                            case 1: i.ConferenceTypeName = "专题会议"; break;
                            case 2: i.ConferenceTypeName = "面试"; break;
                            case 3: i.ConferenceTypeName = "培训"; break;
                            case 4: i.ConferenceTypeName = "客户接待"; break;
                            case 5: i.ConferenceTypeName = "访谈交流"; break;
                            case 6: i.ConferenceTypeName = "外部主场"; break;
                            case 7: i.ConferenceTypeName = "分会场参会"; break;
                            case 8: i.ConferenceTypeName = "其他类型"; break;
                            default: i.ConferenceTypeName = "未知"; break;
                        }
                    });
                    list.RecordCount = RecordCount;
                    list.PageCount = PageCount;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetDeptMeetingReport Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }
        /// <summary>
        /// 获取人员统计详情报表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<PersonMeetingDetailsReport>> GetPersonMeetingDetailsReport(ReportQuery query)
        {
            PageList<PersonMeetingDetailsReport> list = new PageList<PersonMeetingDetailsReport>();
            List<PersonMeetingDetailsReport> listReport = new List<PersonMeetingDetailsReport>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetPersonMeetingDetailsReport";
            LogEntity.OperateObject = "GetPersonMeetingDetailsReport";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<ReportQuery>(query);
            try
            {
                query.BeginTime = Convert.ToDateTime(query.Begin + " 00:00:01");
                query.EndTime = Convert.ToDateTime(query.End + " 23:59:59");
                var (dt, RecordCount, PageCount) = await dal.GetPersonMeetingDetailsReport(query, out strError);
                if (strError == "")
                {
                    listReport = TableToList.ToList<PersonMeetingDetailsReport>(dt);
                    listReport.ForEach(i =>
                    {
                        i.Date = i.BeginTime.ToString("yyyy-MM-dd");
                        i.Begin = i.BeginTime.ToString("HH:mm");
                        i.End = i.EndTime.ToString("HH:mm");

                        switch (i.ConferenceType)
                        {
                            case 0: i.ConferenceTypeName = "纪要未上传"; break;
                            case 1: i.ConferenceTypeName = "专题会议"; break;
                            case 2: i.ConferenceTypeName = "面试"; break;
                            case 3: i.ConferenceTypeName = "培训"; break;
                            case 4: i.ConferenceTypeName = "客户接待"; break;
                            case 5: i.ConferenceTypeName = "访谈交流"; break;
                            case 6: i.ConferenceTypeName = "外部主场"; break;
                            case 7: i.ConferenceTypeName = "分会场参会"; break;
                            case 8: i.ConferenceTypeName = "其他类型"; break;
                            default: i.ConferenceTypeName = "未知"; break;
                        }
                    });
                    list.List = listReport;
                    list.RecordCount = RecordCount;
                    list.PageCount = PageCount;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetPersonMeetingDetailsReport Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }
        /// <summary>
        /// 系统日志
        /// </summary>
        private void RecordLog()
        {
            LogEntity.Operator = Operator;
            LogEntity.OperateIP = OperaIP;
            ULogWork.CLogToDataBase.RecordOperateLog(LogEntity);
        }
    }
}
